package org.shoukaiseki.aqoeous.token;

import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

/**
 * org.shoukaiseki.aqoeous.token.TokenHolder
 * <br>
 *
 * @author 蒋カイセキ    Japan-Tokyo  19-2-2 下午4:07<br>
 * ブログ http://www.shoukaiseki.top/<br>
 * E-メール jiang28555@Gmail.com<br>
 **/
public class TokenHolder  {

  public final static String TOKEN_USER = "tokenUser";


    public static TokenUser get() {
        ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        return (TokenUser) servletRequestAttributes.getRequest().getSession().getAttribute(TOKEN_USER);
    }


    public static void set(TokenUser tokenUser) {
        ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        servletRequestAttributes.getRequest().getSession().setAttribute(TOKEN_USER,tokenUser);
    }

    public static void remove(){
        ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        servletRequestAttributes.getRequest().getSession().removeAttribute(TOKEN_USER);
    }


    /**
     * 获取ID
     * @return
     */
    public static Long getUserId(){
        TokenUser tokenUser = get();
        if(tokenUser!=null&&tokenUser.getUserId()!=null){
            return tokenUser.getUserId();
        }
        return null;
    }


}

